<?php
// +----------------------------------------------------------------------
// | Jqcms [ Avenue to Jane ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://jqcms.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: hyacinth <845077398@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\BaseController;

class AdminController extends BaseController {
    
    public function index(){
        $this->display();
    }
    //
    public function home(){
        
        $info = array(
            'jq_os' => PHP_OS,
    		'jq_hj' => $_SERVER["SERVER_SOFTWARE"],
    	    'jq_php' => PHP_VERSION,
    		'jq_cgi' => php_sapi_name(),
			'jq_dd' => phpversion(),
    	    'jq_mysql' => $this->mysql_version(),
    		'jq_v' => 'JQCMS v'.JQCMS_VERSION . "&nbsp;&nbsp;&nbsp;",
    		'jq_upload_max' => ini_get('upload_max_filesize'),
    		'jq_time' => ini_get('max_execution_time') . "s",
    		'jq_disk' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
        );
        $this->assign('info',$info);
        $this->display();
    }
    //修改密码
    public function pass(){
        
        $pass = D('Admin');
        if(IS_POST){
            
            if($pass->create()){
                $yzpass = $pass->setPass();
                if ($yzpass){
                    $this->success('密码修改成功!');
                }else {
                    $this->error('原密码错误!');
                }
            }else {
                $this->error($pass->getError());
            }
            
        }else {
            $where['name'] = session('name');
            $data = $pass->where($where)->find();
            $this->assign('data',$data);
            $this->display();
        }
         
    }
    //
    public function upgrade(){
        
        $url='http://'.JQCMS_URL.'/Download/Update/';
        $data = file_get_contents($url);
        $data = json_decode($data);
        $oldv = floatval(JQCMS_VERSION);
        $newv = floatval($data->v);
        $file = $data->file;
        if ($newv > $oldv){
            $dfile = new \Org\Net\Http();
            $filename = $dfile->httpcopy($file);
            if($filename){
                $phpzip = new \Org\Util\Phpzip();
                $unzip = $phpzip->unZip($filename, './');
                if ($unzip!=null){
                    unlink($filename);
                    $this->success('恭喜,升级成功!','home');
                }else{
                    $this->error('您的服务器不支持解压，请手动更新文件!','home');
                }
            }else{
                $this->error('获取升级包失败!','home');
            }
        }else{
            $this->success('已是最新版本!','home');
        }
        
    }
    //mysql版本
    private function mysql_version()
    {
        $Model = M();
        $version = $Model->query("select version() as ver");
        return $version[0]['ver'];
    }
    //清除缓存
    public function Clearcache(){
        
        $deldir = delDirAndFile(RUNTIME_PATH);
        $this->success('缓存已清除!');
    
    }
    
    
}